package com.cdtye.itps.cms.service.plan;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdtye.common.mybatiscore.base.BaseService;
import com.cdtye.common.mybatiscore.base.BaseVo;
import com.cdtye.itps.cms.dao.lineSite.vo.JcLineSiteVo;
import com.cdtye.itps.cms.dao.plan.entity.JcjcPlanDetail;
import com.cdtye.itps.cms.dao.plan.vo.JcjcPlanDetailVo;
import com.cdtye.itps.cms.dao.plan.vo.YearPlanManagementIndexVo;
import com.cdtye.itps.cms.dao.result.*;
import com.cdtye.itps.cms.mapper.lineSite.JcLineSiteMapper;
import com.cdtye.itps.cms.mapper.plan.JcjcPlanDetailMapper;
import com.cdtye.itps.cms.service.dept.SysDeptService;
import com.cdtye.itps.cms.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class JcjcPlanDetailService extends BaseService<JcjcPlanDetailMapper, JcjcPlanDetail> {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private JcLineSiteMapper jcLineSiteMapper;

    /**
     * 分页查询
     * @param vo
     * @return
     */
    public IPage<Map<String,Object>> listPage(JcjcPlanDetailVo vo){
        vo.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(vo.getUnitDeptCode()));
        return baseMapper.listPage(vo.page(), vo);
    }

    /**
     *检测计划管理首页-本年计划基本信息
     */
    public Map<String,Object> getYearPlanBasic(JcjcPlanDetailVo vo) {
        vo.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        return baseMapper.getYearPlanBasic(vo);
    }

    /**
     *检测计划管理首页-本年度各单位年检测计划完成率
     */
    public  Map<String,Object> getYearPlanCompletionRate(JcjcPlanDetailVo vo) {
        Map<String,Object> resultMap = baseMapper.getYearPlanCompletionRate(vo);
        return resultMap;
    }

    /**
     *检测计划管理首页-本年度检测计划完成率
     */
    public Map<String, Object> getPlanCompletionRate(JcjcPlanDetailVo vo) {
        vo.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        Map<String,Object> map = baseMapper.getYearPlanBasic(vo);
        int totalNum_1C = Integer.parseInt(map.get("total1c").toString());
        int completeNum_1C = Integer.parseInt(map.get("complete1c").toString());
        int totalNum_2C = Integer.parseInt(map.get("total2c").toString());
        int completeNum_2C = Integer.parseInt(map.get("complete2c").toString());
        int totalNum_4C = Integer.parseInt(map.get("total4c").toString());
        int completeNum_4C = Integer.parseInt(map.get("complete4c").toString());
        Double CompleteRate_1C = 0.00;
        Double CompleteRate_2C = 0.00;
        Double CompleteRate_4C = 0.00;
        if(totalNum_1C!=0&&completeNum_1C!=0){
            CompleteRate_1C=new BigDecimal(completeNum_1C*100).divide(new BigDecimal(totalNum_1C),2,BigDecimal.ROUND_HALF_UP).doubleValue();

        }
        if(totalNum_2C!=0&&completeNum_2C!=0){
            CompleteRate_2C=new BigDecimal(completeNum_2C*100).divide(new BigDecimal(totalNum_2C),2,BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        if(totalNum_4C!=0&&completeNum_4C!=0){
            CompleteRate_4C=new BigDecimal(completeNum_4C*100).divide(new BigDecimal(totalNum_4C),2,BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("CompleteRate_1C", CompleteRate_1C);
        resultMap.put("CompleteRate_2C", CompleteRate_2C);
        resultMap.put("CompleteRate_4C", CompleteRate_4C);
        return resultMap;
    }

    /**
     *检测计划管理首页-检测计划概况
     */
    public Map<String, Object> getYearCheckedSituation(JcjcPlanDetailVo vo) {
        vo.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        YearPlanManagementIndexVo vo_plan = new YearPlanManagementIndexVo();
        YearPlanManagementIndexVo vo_complete = new YearPlanManagementIndexVo();
        YearPlanManagementIndexVo vo_overtime = new YearPlanManagementIndexVo();
        if(vo.getYear()!=null){
            vo_plan.setYear(vo.getYear());
            vo_complete.setYear(vo.getYear());
            vo_overtime.setYear(vo.getYear());
        }
        if(vo.getRailTypeList()!=null && vo.getRailTypeList().size()>0){
            vo_plan.setRailTypeList(vo.getRailTypeList());
            vo_complete.setRailTypeList(vo.getRailTypeList());
            vo_overtime.setRailTypeList(vo.getRailTypeList());
        }
        if(vo.getUnitDeptCode()!=null && vo.getUnitDeptCode()!=""){
            vo_plan.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
            vo_complete.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
            vo_overtime.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        }
        //1C计划数
        vo_plan.setCType("1C");
        List<Map<String,Object>> planNumList_1C = baseMapper.getYearPerMonthPlanNum(vo_plan);
        //2C计划数
        vo_plan.setCType("2C");
        List<Map<String,Object>> planNumList_2C = baseMapper.getYearPerMonthPlanNum(vo_plan);
        //4C计划数
        vo_plan.setCType("4C");
        List<Map<String,Object>> planNumList_4C = baseMapper.getYearPerMonthPlanNum(vo_plan);
        //完成总数
        List<String> listStatus = new ArrayList<>();
        listStatus.add("40");listStatus.add("70");listStatus.add("80");
        vo_complete.setStatusList(listStatus);
        List<Map<String,Object>> completeNumList = baseMapper.getYearPerMonthPlanTotalNum(vo_complete);
        //超期总数
        List<String> overList = new ArrayList<>();
        overList.add("10");overList.add("20");overList.add("30");
        vo_overtime.setStatusList(overList);
        List<Map<String,Object>> planOverTimeNumList = baseMapper.getYearPerMonthPlanOverTimeNum(vo_overtime);

        //1、2、4C计划数
        List<Integer> data_1C = new ArrayList<>();
        List<Integer> data_2C = new ArrayList<>();
        List<Integer> data_4C = new ArrayList<>();
        //完成总数
        List<Integer> data_Complete = new ArrayList<>();
        //超期总数
        List<Integer> data_OverTime = new ArrayList<>();
        //总完成率
        List<Double> data_CompleteRate = new ArrayList<>();

        for (int i = 1; i < 13; i++) {
            Integer num_1C = 0;
            Integer num_2C = 0;
            Integer num_4C = 0;
            Integer num_Complete = 0;
            Integer num_OverTime = 0;
            Double num_CompleteRate = 0.00;
            for (int j = 0; j < planNumList_1C.size(); j++) {
                if(i == Integer.valueOf(planNumList_1C.get(j).get("month").toString())){
                    num_1C = Integer.valueOf(planNumList_1C.get(j).get("num").toString());
                }
            }
            for (int j = 0; j < planNumList_2C.size(); j++) {
                if(i == Integer.valueOf(planNumList_2C.get(j).get("month").toString())){
                    num_2C = Integer.valueOf(planNumList_2C.get(j).get("num").toString());
                }
            }
            for (int j = 0; j < planNumList_4C.size(); j++) {
                if(i == Integer.valueOf(planNumList_4C.get(j).get("month").toString())){
                    num_4C = Integer.valueOf(planNumList_4C.get(j).get("num").toString());
                }
            }
            for (int j = 0; j < completeNumList.size(); j++) {
                if(i == Integer.valueOf(completeNumList.get(j).get("month").toString())){
                    num_Complete = Integer.valueOf(completeNumList.get(j).get("num").toString());
                }
            }
            for (int j = 0; j < planOverTimeNumList.size(); j++) {
                if(i == Integer.valueOf(planOverTimeNumList.get(j).get("month").toString())){
                    num_OverTime = Integer.valueOf(planOverTimeNumList.get(j).get("num").toString());
                }
            }
            int totalNum =  num_1C + num_2C + num_4C;
            if(num_Complete!=0&&totalNum!=0){
                num_CompleteRate = Double.valueOf(num_Complete) / Double.valueOf(totalNum);
            }
            data_1C.add(num_1C);
            data_2C.add(num_2C);
            data_4C.add(num_4C);
            data_Complete.add(num_Complete);
            data_OverTime.add(num_OverTime);
            BigDecimal num_CompleteRate_tag = new BigDecimal(num_CompleteRate);
            data_CompleteRate.add(num_CompleteRate_tag.setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue()*100);
        }
        Map<String, Object> resultMap = new HashMap<>(9);
        resultMap.put("data_1C", data_1C);
        resultMap.put("data_2C", data_2C);
        resultMap.put("data_4C", data_4C);
        resultMap.put("data_Complete", data_Complete);
        resultMap.put("data_OverTime", data_OverTime);
        resultMap.put("data_CompleteRate", data_CompleteRate);
        return resultMap;
    }

    /**
     *检测计划管理首页-预警信息
     */
    public List<Map<String,Object>> getEarlyWarning(YearPlanManagementIndexVo vo) {
        List<Map<String,Object>>  dataList = baseMapper.getEarlyWarning(vo);
        return dataList;
    }

    /**
     *首页-超期预警情况-计划
     */
    public Map<String, Object> getPlanOverWarn(JcjcPlanDetailVo req){
        req.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        return baseMapper.getPlanOverWarn(req);
    }

    /**
     *首页-检测计划兑现详情-周期检测执行情况
     */
    public CheckPlanCashDetailResVo getCheckPlanCashDetail(JcjcPlanDetailVo reqVo){
        reqVo.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        CheckPlanCashDetailResVo result=new CheckPlanCashDetailResVo();
        if(StringUtils.isBlank(reqVo.getYear())){
            reqVo.setYear(DateUtils.getCurrentYear());
        }
        List<Map> list=baseMapper.overdueUnexecutedNum(reqVo);
        List<OverdueUnexecutedNumResVo> listN=new ArrayList<>();
        List<String> listMonth= DateUtils.getYearAllMonth(reqVo.getYear());
        result.setSum(0);
        result.setUnexecutedNum(0);
        listMonth.stream().forEach(a->{
            OverdueUnexecutedNumResVo numResVo=new OverdueUnexecutedNumResVo();
            numResVo.setMonth(a);
            numResVo.setSumNum(0);
            numResVo.setUnExecutedNum(0);
            list.stream().forEach(b->{
                if(a.equals(b.get("yearMonth").toString())){
                    numResVo.setUnExecutedNum(Integer.parseInt(b.get("unExecutedNum").toString()));
                    numResVo.setSumNum(Integer.parseInt(b.get("sumNum").toString()));
                    result.setSum(result.getSum()+numResVo.getSumNum());
                    result.setUnexecutedNum(result.getUnexecutedNum()+numResVo.getUnExecutedNum());
                }
            });
            listN.add(numResVo);
        });
        result.setList(listN);
        return result;
    }

    /**
     *首页-检测计划兑现详情-超期未执行线路Top5
     */
    public List<UnexecutedLineResVo> getUnexecutedLineTop5(JcjcPlanDetailVo reqVo){
        reqVo.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        List<UnexecutedLineResVo> list=new ArrayList<>();
        BaseVo p=new BaseVo();
        p.setPage(1);
        p.setSize(5);
        IPage<Map<String,Object>> page= baseMapper.overdueUnexecutedLine(p.page(),reqVo);
        page.getRecords().stream().forEach(a->{
            UnexecutedLineResVo resVo=new UnexecutedLineResVo();
            resVo.setLineName(a.get("lineName").toString());
            resVo.setNum(Integer.parseInt(a.get("num").toString()));
            list.add(resVo);
        });
        return list;
    }

    /**
     * 首页-检测计划完成率
     */
    public TestPlanCompleteRateResVo getTestPlanCompleteRate(JcjcPlanDetailVo vo){
        vo.setUnitDeptCode(sysDeptService.getUnitDeptCodeByLoginUser(null));
        Map<String,Object> resultMap = baseMapper.getYearPlanBasic(vo);
        TestPlanCompleteRateResVo testPlanCompleteRate=new TestPlanCompleteRateResVo();
        Integer complete1C = Integer.valueOf(resultMap.get("complete1c").toString());
        Integer sum1C = Integer.valueOf(resultMap.get("total1c").toString());
        Integer complete2C = Integer.valueOf(resultMap.get("complete2c").toString());
        Integer sum2C = Integer.valueOf(resultMap.get("total2c").toString());
        Integer complete4C = Integer.valueOf(resultMap.get("complete4c").toString());
        Integer sum4C = Integer.valueOf(resultMap.get("total4c").toString());

        testPlanCompleteRate.setComplete1C(complete1C.toString());
        testPlanCompleteRate.setSum1C(sum1C.toString());
        testPlanCompleteRate.setCompleteRate1C("0");
        if(!testPlanCompleteRate.getSum1C().equals("0")){
            testPlanCompleteRate.setCompleteRate1C(new BigDecimal(complete1C*100).divide(new BigDecimal(sum1C),2,BigDecimal.ROUND_HALF_UP).toString());
        }

        testPlanCompleteRate.setComplete2C(complete2C.toString());
        testPlanCompleteRate.setSum2C(sum2C.toString());
        testPlanCompleteRate.setCompleteRate2C("0");
        if(!testPlanCompleteRate.getSum2C().equals("0")){
            testPlanCompleteRate.setCompleteRate2C(new BigDecimal(complete2C*100).divide(new BigDecimal(sum2C),2,BigDecimal.ROUND_HALF_UP).toString());
        }

        testPlanCompleteRate.setComplete4C(complete4C.toString());
        testPlanCompleteRate.setSum4C(sum4C.toString());
        testPlanCompleteRate.setCompleteRate4C("0");
        if(!testPlanCompleteRate.getSum4C().equals("0")){
            testPlanCompleteRate.setCompleteRate4C(new BigDecimal(complete4C*100).divide(new BigDecimal(sum4C),2,BigDecimal.ROUND_HALF_UP).toString());
        }
        return testPlanCompleteRate;
    }

    /**
     * 首页-周期检测覆盖
     */
    public List<CycleCheckCoverResVo> getCycleCheckCover(JcjcPlanDetailVo vo){
        List<CycleCheckCoverResVo> result = new ArrayList<>();
        List<Map<String, Object>> lineSiteList = jcLineSiteMapper.getList(new JcLineSiteVo());
        Map<String, List<Map<String, Object>>> seclineIdMap = lineSiteList.stream().collect(Collectors.groupingBy(s->s.get("seclineId").toString()));
        //1c
        vo.setCType("1C");
        CycleCheckCoverResVo resultC1 = getCycleCheckCoverByCType(vo,seclineIdMap);
        //2c
        vo.setCType("2C");
        CycleCheckCoverResVo resultC2 = getCycleCheckCoverByCType(vo,seclineIdMap);
        //4c
        vo.setCType("4C");
        CycleCheckCoverResVo resultC4 = getCycleCheckCoverByCType(vo,seclineIdMap);

        result.add(resultC1);
        result.add(resultC2);
        result.add(resultC4);
        return result;
    }

    private CycleCheckCoverResVo getCycleCheckCoverByCType(JcjcPlanDetailVo vo,Map<String, List<Map<String, Object>>> seclineIdMap){
        CycleCheckCoverResVo result = baseMapper.actualCover(vo);
        result.setCTypeName(vo.getCType());
        List<Map<String, Object>> list = baseMapper.getList(vo);
        for (Map<String, Object> plan : list) {
            String seclineId = plan.get("lineId").toString();
            String lineSitePlanBeginId = plan.get("lineSitePlanBeginId")!=null ? plan.get("lineSitePlanBeginId").toString():null;
            String lineSitePlanEndId = plan.get("lineSitePlanEndId")!=null ? plan.get("lineSitePlanEndId").toString():null;
            String lineSiteActualBeginId = plan.get("lineSiteActualBeginId")!=null ? plan.get("lineSiteActualBeginId").toString():null;
            String lineSiteActualEndId = plan.get("lineSiteActualEndId")!=null ? plan.get("lineSiteActualEndId").toString():null;
            List<Map<String, Object>> lineSiteList = seclineIdMap.get(seclineId);
            int planBeginIndex = getIndexByLineSiteId(lineSiteList,lineSitePlanBeginId);
            int planEndIndex = getIndexByLineSiteId(lineSiteList,lineSitePlanEndId);
            int actualBeginIndex = getIndexByLineSiteId(lineSiteList,lineSiteActualBeginId);
            int actualEndIndex = getIndexByLineSiteId(lineSiteList,lineSiteActualEndId);
            int planNum =planBeginIndex == planEndIndex ? 0 : Math.abs((planEndIndex - planBeginIndex) + 1);
            int actualNum = actualBeginIndex == actualEndIndex ? 0 : Math.abs((actualEndIndex-actualBeginIndex) + 1);
            result.setNotCoverSiteNum(result.getNotCoverSiteNum()+Math.abs(planNum-actualNum));
        }
        return result;
    }

    private int getIndexByLineSiteId(List<Map<String, Object>> lineSiteList, String lineSiteId){
        if(null == lineSiteId || null == lineSiteList) return -1;
        for(int i=0;i<lineSiteList.size();i++){
            Map<String,Object> lineSite = lineSiteList.get(i);
            if(lineSiteId.equals(lineSite.get("siteId").toString())){
                return i;
            }
        }
        return -1;
    }

    public List<UncoveredAreaStationLineNameReqVo> getUncoveredAreaStation(JcjcPlanDetailVo vo){
        List<UncoveredAreaStationLineNameReqVo> result= new ArrayList<>();
        List<Map<String, Object>> lineSiteList = jcLineSiteMapper.getList(new JcLineSiteVo());
        Map<String, List<Map<String, Object>>> lineSiteListGroupByLine = lineSiteList.stream().collect(Collectors.groupingBy(s->s.get("seclineId").toString()));
        List<Map<String, Object>> planList = baseMapper.getList(vo);
        Map<String, List<Map<String, Object>>> planListGroupByLine = planList.stream().collect(Collectors.groupingBy(s->s.get("lineId").toString()));
        for(Map.Entry<String, List<Map<String, Object>>> entry : planListGroupByLine.entrySet()){
            String seclineId = null;
            String lineName = null;
            int num = 0;
            for (Map<String, Object> plan : entry.getValue()) {
                 seclineId = plan.get("lineId").toString();
                 lineName = plan.get("lineName").toString();
                String lineSitePlanBeginId = plan.get("lineSitePlanBeginId")!=null ? plan.get("lineSitePlanBeginId").toString():null;
                String lineSitePlanEndId = plan.get("lineSitePlanEndId")!=null ? plan.get("lineSitePlanEndId").toString():null;
                String lineSiteActualBeginId = plan.get("lineSiteActualBeginId")!=null ? plan.get("lineSiteActualBeginId").toString():null;
                String lineSiteActualEndId = plan.get("lineSiteActualEndId")!=null ? plan.get("lineSiteActualEndId").toString():null;
                List<Map<String, Object>> lineSiteList2 = lineSiteListGroupByLine.get(seclineId);
                int planBeginIndex = getIndexByLineSiteId(lineSiteList2,lineSitePlanBeginId);
                int planEndIndex = getIndexByLineSiteId(lineSiteList2,lineSitePlanEndId);
                int actualBeginIndex = getIndexByLineSiteId(lineSiteList2,lineSiteActualBeginId);
                int actualEndIndex = getIndexByLineSiteId(lineSiteList2,lineSiteActualEndId);
                int planNum =planBeginIndex == planEndIndex ? 0 : Math.abs((planEndIndex - planBeginIndex) + 1);
                int actualNum = actualBeginIndex == actualEndIndex ? 0 : Math.abs((actualEndIndex-actualBeginIndex) + 1);
                num = num + Math.abs(planNum-actualNum);
            }
            if(null != seclineId && null !=lineName){
                UncoveredAreaStationLineNameReqVo uncoveredAreaStationLineNameReqVo = new UncoveredAreaStationLineNameReqVo();
                uncoveredAreaStationLineNameReqVo.setLineId(seclineId);
                uncoveredAreaStationLineNameReqVo.setLineName(lineName);
                uncoveredAreaStationLineNameReqVo.setNum(num);
                result.add(uncoveredAreaStationLineNameReqVo);
            }
        }
        return result;
    }

    public UncoveredAreaStationDetailResVo getUncoveredAreaStationDetail(JcjcPlanDetailVo vo) throws Exception {

        UncoveredAreaStationDetailResVo result = new UncoveredAreaStationDetailResVo();
        List<String> monthList = DateUtils.getYearMonth(vo.getBeginYearMonth(), vo.getEndYearMonth());
        Map<String, List<Map<String, Object>>> lineSiteListGroupByLine = jcLineSiteMapper.getList(new JcLineSiteVo()).stream().collect(Collectors.groupingBy(s -> s.get("seclineId").toString()));
        List<Map<String, Object>> lineSiteList = lineSiteListGroupByLine.get(vo.getLineId());

        List<UncoveredAreaStationSiteNameReqVo> uncoveredAreaStationSiteNameReqVoList = buildUncoveredAreaStationSiteNameReqVoList(vo,lineSiteList);
        List<Map<String, Object>> planList2 = baseMapper.getList(vo);
        for (Map<String, Object> plan : planList2) {
            String lineSitePlanBeginId = plan.get("lineSitePlanBeginId") != null ? plan.get("lineSitePlanBeginId").toString() : null;
            String lineSitePlanEndId = plan.get("lineSitePlanEndId") != null ? plan.get("lineSitePlanEndId").toString() : null;
            String lineSiteActualBeginId = plan.get("lineSiteActualBeginId") != null ? plan.get("lineSiteActualBeginId").toString() : null;
            String lineSiteActualEndId = plan.get("lineSiteActualEndId") != null ? plan.get("lineSiteActualEndId").toString() : null;
            int planBeginIndex = getIndexByLineSiteId(lineSiteList, lineSitePlanBeginId);
            int planEndIndex = getIndexByLineSiteId(lineSiteList, lineSitePlanEndId);
            int actualBeginIndex = getIndexByLineSiteId(lineSiteList, lineSiteActualBeginId);
            int actualEndIndex = getIndexByLineSiteId(lineSiteList, lineSiteActualEndId);
            uncoveredAreaStationSiteNameReqVoList = updateUncoveredAreaStationSiteNameReqVoList(uncoveredAreaStationSiteNameReqVoList,lineSiteList, planBeginIndex, planEndIndex, actualBeginIndex, actualEndIndex);
        }

        List<UncoveredAreaStationSiteNameDetailReqVo> uncoveredAreaStationSiteNameDetailReqVoList = buildUncoveredAreaStationSiteNameDetailReqVoList(vo, monthList, lineSiteList);
        for (int i = 0; i < monthList.size(); i++) {
            String month = monthList.get(i);
            vo.setYearMonth(month);
            List<Map<String, Object>> planList = baseMapper.getList(vo);
            for (Map<String, Object> plan : planList) {
                String lineSitePlanBeginId = plan.get("lineSitePlanBeginId") != null ? plan.get("lineSitePlanBeginId").toString() : null;
                String lineSitePlanEndId = plan.get("lineSitePlanEndId") != null ? plan.get("lineSitePlanEndId").toString() : null;
                String lineSiteActualBeginId = plan.get("lineSiteActualBeginId") != null ? plan.get("lineSiteActualBeginId").toString() : null;
                String lineSiteActualEndId = plan.get("lineSiteActualEndId") != null ? plan.get("lineSiteActualEndId").toString() : null;
                int planBeginIndex = getIndexByLineSiteId(lineSiteList, lineSitePlanBeginId);
                int planEndIndex = getIndexByLineSiteId(lineSiteList, lineSitePlanEndId);
                int actualBeginIndex = getIndexByLineSiteId(lineSiteList, lineSiteActualBeginId);
                int actualEndIndex = getIndexByLineSiteId(lineSiteList, lineSiteActualEndId);
                uncoveredAreaStationSiteNameDetailReqVoList = updateUncoveredAreaStationSiteNameDetailReqVoList(i, uncoveredAreaStationSiteNameDetailReqVoList, planBeginIndex, planEndIndex, actualBeginIndex, actualEndIndex);
            }
        }


        result.setSiteNameYearList(uncoveredAreaStationSiteNameDetailReqVoList);
        result.setSiteNameList(uncoveredAreaStationSiteNameReqVoList);
        return result;
    }

    private List<UncoveredAreaStationSiteNameReqVo> updateUncoveredAreaStationSiteNameReqVoList(List<UncoveredAreaStationSiteNameReqVo> uncoveredAreaStationSiteNameReqVoList, List<Map<String, Object>> lineSiteList, int planBeginIndex, int planEndIndex, int actualBeginIndex, int actualEndIndex) {
        for (UncoveredAreaStationSiteNameReqVo uncoveredAreaStationSiteNameReqVo : uncoveredAreaStationSiteNameReqVoList) {
            int index = uncoveredAreaStationSiteNameReqVo.getIndex();
            if(index>=planBeginIndex && index<=planEndIndex){
                if((actualBeginIndex == -1 || actualEndIndex == -1)|| (index<actualBeginIndex &&index>actualEndIndex)){
                    uncoveredAreaStationSiteNameReqVo.setNum(uncoveredAreaStationSiteNameReqVo.getNum()+1);
                }
            }
        }
        uncoveredAreaStationSiteNameReqVoList = uncoveredAreaStationSiteNameReqVoList.stream().sorted(Comparator.comparing(UncoveredAreaStationSiteNameReqVo::getNum).reversed()).collect(Collectors.toList());
        return uncoveredAreaStationSiteNameReqVoList;
    }

    private List<UncoveredAreaStationSiteNameDetailReqVo> updateUncoveredAreaStationSiteNameDetailReqVoList(int monthIndex, List<UncoveredAreaStationSiteNameDetailReqVo> uncoveredAreaStationSiteNameDetailReqVoList, int planBeginIndex, int planEndIndex, int actualBeginIndex, int actualEndIndex) {
        for (UncoveredAreaStationSiteNameDetailReqVo item : uncoveredAreaStationSiteNameDetailReqVoList) {
            if(item.getIndex()>= planBeginIndex && item.getIndex()<=planEndIndex){
                item.getList().get(monthIndex).setPlanCount(item.getList().get(monthIndex).getPlanCount()+1);
            }
            if(item.getIndex()>= actualBeginIndex && item.getIndex()<=actualEndIndex){
                item.getList().get(monthIndex).setCheckCount(item.getList().get(monthIndex).getCheckCount()+1);
            }
        }
        return uncoveredAreaStationSiteNameDetailReqVoList;
    }

    private List<UncoveredAreaStationSiteNameReqVo> buildUncoveredAreaStationSiteNameReqVoList(JcjcPlanDetailVo vo, List<Map<String, Object>> lineSiteList) {
        List<UncoveredAreaStationSiteNameReqVo> uncoveredAreaStationSiteNameReqVoList = new ArrayList<>();
        for (int i = 0; i < lineSiteList.size(); i++) {
            Map<String, Object> lineSite = lineSiteList.get(i);
            UncoveredAreaStationSiteNameReqVo uncoveredAreaStationSiteNameReqVo = new UncoveredAreaStationSiteNameReqVo();
            uncoveredAreaStationSiteNameReqVo.setSiteName(lineSite.get("siteName").toString());
            uncoveredAreaStationSiteNameReqVo.setSiteId(lineSite.get("siteId").toString());
            uncoveredAreaStationSiteNameReqVo.setIndex(i);
            uncoveredAreaStationSiteNameReqVoList.add(uncoveredAreaStationSiteNameReqVo);
        }
        return uncoveredAreaStationSiteNameReqVoList;
    }


    private List<UncoveredAreaStationSiteNameDetailReqVo> buildUncoveredAreaStationSiteNameDetailReqVoList(JcjcPlanDetailVo vo,List<String> monthList,List<Map<String, Object>> lineSiteList) throws Exception {
        List<UncoveredAreaStationSiteNameDetailReqVo> uncoveredAreaStationSiteNameDetailReqVoList = new ArrayList<>();
        for (int i = 0; i < lineSiteList.size(); i++) {
            Map<String, Object> lineSite = lineSiteList.get(i);
            UncoveredAreaStationSiteNameDetailReqVo reqvo = new UncoveredAreaStationSiteNameDetailReqVo();
            reqvo.setSiteId(lineSite.get("siteId").toString());
            reqvo.setSiteName(lineSite.get("siteName").toString());
            reqvo.setIndex(i);
            List<SiteNameDetail> siteNameDetails = new ArrayList<>();
            monthList.forEach(month->{
                SiteNameDetail siteNameDetail = new SiteNameDetail();
                siteNameDetail.setCheckMonth(month);
                siteNameDetail.setSiteName(lineSite.get("siteName").toString());
                siteNameDetails.add(siteNameDetail);
            });
            reqvo.setList(siteNameDetails);
            uncoveredAreaStationSiteNameDetailReqVoList.add(reqvo);
        }
        return uncoveredAreaStationSiteNameDetailReqVoList;
    }

}
